package com.woniuxy.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RoleMapper;
import com.woniuxy.dao.RolePmMapper;
import com.woniuxy.pojo.Role;
import com.woniuxy.pojo.RolePmKey;
import com.woniuxy.service.system.RoleService;
@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RolePmMapper rolePmMapper;
	@Override
	public PageInfo selectAll(Integer pn, String condition) {
		PageHelper.startPage(pn, 5);
		List<Role> list = roleMapper.selectAll(condition);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}
	@Override
	public void addRole(Role role) {
		roleMapper.insert(role);
		
	}
	@Override
	public void updateRole(Role role) {
		roleMapper.updateByPrimaryKey(role);
		
	}
	@Override
	public void updatePersByRoleId(Integer rid, String pids) {
		rolePmMapper.deleteByRoleId(rid);
		String[] strs = pids.split(",");
		List<RolePmKey> list = new ArrayList<RolePmKey>();
		for (String str : strs) {
			RolePmKey key = new RolePmKey();
			key.setRoleId(rid);
			key.setPmId(Integer.parseInt(str));
			list.add(key);
		}
		rolePmMapper.insertByPids(list);
		
	}
	@Override
	public List<Role> selectAllRoles() {
		// TODO Auto-generated method stub
		return roleMapper.selectAllRoles();
	}

}
